<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/7/3
 * Time: 下午5:43
 */

namespace app\src\deleteApply\logic;


use app\src\base\logic\BaseLogic;
use app\src\deleteApply\enum\DeleteApplyType;
use app\src\deleteApply\model\DeleteApply;
use app\src\platform\logic\PlatformLogic;
use app\src\seller\logic\SellerLogic;
use think\Db;

class DeleteApplyLogic extends BaseLogic
{

    protected function _init()
    {
        $this->setModel(new DeleteApply());
    }

    /**
     * 添加申请
     * @param $type
     * @param $item_id
     * @param $applicant_account_id
     * @return array
     */
    public function addApply($type, $item_id, $applicant_account_id)
    {

        if(!in_array($type, [DeleteApplyType::SELLER, DeleteApplyType::PLATFORM])){
            return $this->apiReturnErr('type is error');
        }

        //判断有没有未处理的相同申请
        $map = [
            'type' => $type,
            'item_id' => $item_id,
            'applicant_account_id' => $applicant_account_id,
            'apply_status' => 0,
            'status' => 0
        ];

        $result = $this->count($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if($result['info'] > 0) return $this->apiReturnErr('已经有相同申请，请等待上一个申请处理完毕！');

        $data = [
            'type' => $type,
            'item_id' => $item_id,
            'applicant_account_id' => $applicant_account_id,
            'create_time' => NOW_TIME,
            'update_time' => NOW_TIME,
            'apply_status' => 0,
            'status' => 0
        ];

        $result = $this->add($data);
        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 申请列表
     * @param $map
     * @param $page
     * @param int $apply_status
     * @return array
     */
    public function applyList($page, $apply_status = 0)
    {
        $map = [
            'status' => 0
        ];

        if($apply_status!==false){
            $map['apply_status'] = $apply_status;
        }

        $order = ['create_time' => 'desc'];
        $result = $this->queryWithCount($map, $page, $order);

        if($result['status']){
            $info = $result['info'];
            foreach ($info['list'] as &$apply){
                //查询被申请删除平台/商家名称
                $item_name = '未知';
                switch ($apply['type']){
                    case DeleteApplyType::SELLER:
                        $result = (new SellerLogic)->getInfo(['id' => $apply['item_id']]);
                        if($result['status'] && !empty($result['info'])){
                            $item_name = $result['info']['name'];
                        }
                        $apply['item_type'] = '商家';
                        break;
                    case DeleteApplyType::PLATFORM:
                        $result = (new PlatformLogic)->getInfo(['id' => $apply['item_id']]);
                        if($result['status'] && !empty($result['info'])){
                            $item_name = $result['info']['name'];
                        }
                        $apply['item_type'] = '平台';
                        break;
                }
                $apply['item_name'] = $item_name;

                //查询申请人名称
                $result = (new PlatformLogic)->getPlatformInfo($apply['applicant_account_id']);
                if($result['status']){
                    $apply['applicant_name'] = $result['info']['name'];
                }else{
                    $apply['applicant_name'] = '未知';
                }
            }
            return $this->apiReturnSuc($info);
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 通过申请
     * @param $apply_id
     * @return array
     */
    public function passApply($apply_id)
    {
        $map = [
            'id' => $apply_id,
            'status' => 0,
            'apply_status' => 0
        ];
        $result = $this->getInfo($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(empty($result['info'])) return $this->apiReturnErr('error apply');

        //判断目标是否已经被删除
        $apply = $result['info'];

        switch ($apply['type']){
            case DeleteApplyType::SELLER:
                $result = (new SellerLogic)->getSellerInfo($apply['item_id']);
                if(!$result['status']) return $this->apiReturnErr($result['info']);

                Db::startTrans();
                $result = $this->saveByID($apply_id, ['apply_status' => 1, 'update_time' => NOW_TIME]);
                if(!$result['status']){
                    Db::rollback();
                    return $this->apiReturnErr($result['info']);
                }
                //删除商家
                $result = (new SellerLogic)->deleteSeller($apply['item_id']);
                if($result['status']){
                    Db::commit();
                    return $this->apiReturnSuc('success');
                }else{
                    Db::rollback();
                    return $this->apiReturnErr($result['info']);
                }
                break;
            case DeleteApplyType::PLATFORM:
                $result = (new PlatformLogic)->getPlatformInfo($apply['item_id']);
                if(!$result['status']) return $this->apiReturnErr($result['info']);

                Db::startTrans();
                $result = $this->saveByID($apply_id, ['apply_status' => 1, 'update_time' => NOW_TIME]);
                if(!$result['status']){
                    Db::rollback();
                    return $this->apiReturnErr($result['info']);
                }
                //删除平台
                $result = (new PlatformLogic)->deletePlatform($apply['item_id']);
                if($result['status']){
                    Db::commit();
                    return $this->apiReturnSuc('success');
                }else{
                    Db::rollback();
                    return $this->apiReturnErr($result['info']);
                }
                break;
            default:
                return $this->apiReturnErr('错误的申请类型,操作失败');
        }
    }

    /**
     * 否决申请
     * @param $apply_id
     * @return array
     */
    public function rejectApply($apply_id)
    {
        $map = [
            'id' => $apply_id,
            'status' => 0,
            'apply_status' => 0
        ];
        $result = $this->getInfo($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(empty($result['info'])) return $this->apiReturnErr('error apply');

        $result = $this->saveByID($apply_id, ['apply_status' => 2, 'update_time' => NOW_TIME]);

        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }
}